package com.zr.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zr.dao.UserDaoHyx;
import com.zr.model.User;
import com.zr.util.JDBCUtil;

public class UserDaoImplHyx implements UserDaoHyx{
	
	/**
	 * 修改老师登录密码
	 */
	@Override
	public boolean updateTeacherPswByTid(int tid, String upsw) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE `user` SET `user`.upsw=?");
		sql.append("WHERE `user`.tid=?");
		Connection con = JDBCUtil.getConnection();
	 	try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, upsw);
			pst.setInt(2, tid);
			int i = pst.executeUpdate();
			if(i>0){
				flag=true;
			}
			
			JDBCUtil.closeJDBC(pst, con);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 	return flag;
	}

//---------创建新的学生用户--------
	@Override
	public boolean addUserForNewStudent(String uname, String upsw, int rid) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer("");
		sql.append(" INSERT into `user`(uname,upsw,rid)");
		sql.append(" VALUES(?,?,?)");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, uname);
			pst.setString(2, upsw);
			pst.setInt(3, rid);
			int i =pst.executeUpdate();
			if(i>0){
				flag=true;
			}
			
			JDBCUtil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return flag;
	}
	public static void main(String[] args) {
		System.out.println(new UserDaoImplHyx().addUserForNewStudent("jiujiu", "123", 3));
	}
	@Override
	public User findNewUserForStudent(String uname, String upsw) {
		User user = new User();
		StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT `user`.uid FROM `user`");
		sql.append(" WHERE `user`.uname=? AND `user`.upsw=?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, uname);
			pst.setString(2, upsw);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				user.setUid(rs.getInt("uid"));
			}
			
			JDBCUtil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return user;
	}
//-------为新学生用户关联对应学生--------
	@Override
	public boolean updateUserTidForNewStudent(int uid, int sid) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE `user` SET `user`.sid=?");
		sql.append(" WHERE `user`.uid=?");
		Connection con = JDBCUtil.getConnection();
	 	try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, sid);
			pst.setInt(2, uid);
			int i = pst.executeUpdate();
			if(i>0){
				flag=true;
			}
			
			JDBCUtil.closeJDBC(pst, con);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 	return flag;
	}
//---------删除学生用户----------
	@Override
	public boolean deleteStudentUser(int sid) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append(" delete from `user` where `user`.sid=?");
		Connection con = JDBCUtil.getConnection();
	 	try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, sid);
			int i = pst.executeUpdate();
			if(i>=0){
				flag=true;
			}
			
			JDBCUtil.closeJDBC(pst, con);
	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 	return flag;
	}
}
